#include "ClientItem.h"
#include "ReadFile.h"

ClientItem::ClientItem(QWidget *parent)
    : QWidget{parent}
{
    this->setFixedSize(240,65);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setStyleSheet(ReadFile::getIntance()->readQssFile(":/qss/clientItem.qss"));
}

void ClientItem::setClientIP(QString ip)
{
    m_ClientIP = ip;
    update();
}

void ClientItem::setClientName(QString name)
{
    m_ClientName = name;
    update();
}

void ClientItem::setOnLineStat(bool isOnline)
{
    m_IsOnLine = isOnline;
    update();
}

void ClientItem::setUnReadMsg()
{
    m_UnReadMsgNum++;
    update();
}

void ClientItem::setUnSelecte()
{
    m_IsSelected = false;
    update();
}

QString ClientItem::getClientIP()
{
    return m_ClientIP;
}

QString ClientItem::getClientName()
{
    return m_ClientName;
}

void ClientItem::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 开启抗锯齿

    switch (m_MouseStat) {
    case E_ENTER:
        painter.setBrush(QBrush(QColor("#f5f7fa")));
        break;
    case E_LEAVE:
        painter.setBrush(QBrush(QColor("#ffffff")));
        break;
    default:
        break;
    }

    if(m_IsSelected) painter.setBrush(QBrush(QColor("#4a90e2")));

    painter.setPen(Qt::transparent);
    QRect rect = this->rect();
    rect.setWidth(rect.width() - 1);
    rect.setHeight(rect.height() - 1);
    painter.drawRoundedRect(rect, 10, 10);
    QWidget::paintEvent(event);

    QPen pen(QColor("#e4e7ed"));
    QBrush brush(QColor("#e4e7ed"));
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(15,15,40,40);

    drawClientStat(painter);
    drawClientIP(painter);
    drawUnReadMsgNum(painter);
}

void ClientItem::enterEvent(QEvent *event)
{
    if(m_IsSelected) return;
    m_MouseStat = E_ENTER;
    update();
}

void ClientItem::leaveEvent(QEvent *event)
{
    if(m_IsSelected) return;
    m_MouseStat = E_LEAVE;
    update();
}

void ClientItem::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        emit this->signal_OpenChatWidget(m_ClientIP);
        m_IsSelected = true;
        m_MouseStat = E_LEAVE;
        m_UnReadMsgNum = 0;
        update();
    }
}

void ClientItem::drawClientStat(QPainter &painter)
{
    QBrush brush(QColor("#7ed321"));
    // 创建 QPen 以控制边框
    QPen pen(QColor("#ffffff"));  // 设置边框颜色
    pen.setWidth(2);     // 设置边框宽度
    pen.setStyle(Qt::SolidLine);  // 线条样式（实线）
    painter.setPen(pen);
    if (m_IsOnLine)
    {
        brush.setColor(QColor("#7ed321"));
    }
    else
    {
        brush.setColor(QColor("#ff2700"));
    }
    painter.setBrush(brush);
    painter.drawEllipse(43,48,8,8);
}

void ClientItem::drawClientIP(QPainter &painter)
{
    QPen pen(QColor("#888888"));
    if(m_IsSelected) pen.setColor(QColor("#b4b0b0"));
    painter.setFont(QFont("sans-serif", 10)); // 设置字体样式
    painter.setPen(pen);

    painter.drawText(75, 53, m_ClientIP);

    pen.setColor(Qt::black);
    if(m_IsSelected) pen.setColor(Qt::white);
    painter.setFont(QFont("system-ui", 15)); // 设置字体样式
    painter.setPen(pen);
    painter.drawText(75, 36, m_ClientName);

}

void ClientItem::drawUnReadMsgNum(QPainter &painter)
{
    if(m_UnReadMsgNum == 0) return;
    QPen pen(Qt::red);
    QBrush brush(Qt::red);
    painter.setPen(pen);
    painter.setBrush(brush);
    painter.drawEllipse(210,26,10,10);
}

void ClientItem::drawBackground(QPainter &painter)
{
    if(!m_IsSelected) return;
}


